package org.myxdb.server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.XmlRpcHandler;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.XmlRpcServletServer;
import org.myxdb.server.message.MessageInterface;

public class MyxdbServer extends HttpServlet {

	   private XmlRpcServletServer server;

	      public void init(ServletConfig pConfig) throws ServletException {

	    	  super.init(pConfig);
	   
	            server = new XmlRpcServletServer();

				  // set up handler mapping of XmlRpcServletServer object
				  XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl)server.getConfig();

				  serverConfig.setEnabledForExtensions(true);

				  serverConfig.setContentLengthOptional(false);
				
				  serverConfig.setEncoding("utf-8");
				  
				  serverConfig.setKeepAliveEnabled(true);
				
				  
				  this.server = new XmlRpcServletServer();
				
				  this.server.setConfig(serverConfig);
				  
				  this.server.setHandlerMapping(new XmlRpcHandlerMapping() {
				  
					  public XmlRpcHandler getHandler(String s) throws XmlRpcException {
				      
						  return MessageInterface.getHandler();
				      }
				  });
				  // more config of XmlRpcServletServer object      

	      }

	       public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)

	            throws IOException, ServletException {

	            server.execute(pRequest, pResponse);

	      }


	
	
}
